package com.hooper.wallet.controller;

import com.hooper.wallet.domain.vo.TransactRecordVo;
import com.hooper.wallet.service.TransactRecordService;
import com.hooper.wallet.service.UserWalletService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static com.hooper.wallet.constant.BalanceConstant.*;

/**
 * @author Tim Hooper
 * @version 1.0
 * @time 2023/03/07/20:31
 */
@Slf4j
@RestController
@RequestMapping("/user/transact")
public class UserTransactController {

    @Resource
    private UserWalletService userWalletService;

    @Resource
    private TransactRecordService transactRecordService;

    //用户id一般在token中获取，故使用随机数/固定数据来替代
    private long getUserId() {
//        Random random = new Random();
//        return random.nextInt(10) + 1;
        return 1L;
    }

    //支付100的功能
    @PostMapping("pay/100")
    public String pay() {
        long userId = getUserId();
        //默认支付100
        return userWalletService.costMoney(userId, DEFAULT_COST);
    }


    //退款20的方法
    @PostMapping("refund")
    public String refund() {
        return userWalletService.refundMoney( getUserId(), DEFAULT_REFUND);
    }

    //提现
    @PostMapping("withdraw/{amount}")
    public String withdraw(@PathVariable Integer amount) {
        return userWalletService.withdraw(getUserId(), amount);
    }

    //获取余额
    @GetMapping("get/balance")
    public String getBalance() {
        return userWalletService.checkBalance(getUserId());
    }

    //获取交易记录
    @GetMapping("/records")
    public List<TransactRecordVo> getRecords() {
        return transactRecordService.getTransactRecords(getUserId());
    }
}
